package com.lcc.codehelper.util;

import java.io.*;

public class FileUtil {

    /**
     * 写出文件，没文件就创建
     * @param file
     * @param content
     * @param fileEncoding
     * @throws IOException
     */
    public static void writeFile(File file, String content, String fileEncoding) throws IOException {
        if(!file.getParentFile().exists()){
            try {
                file.getParentFile().mkdirs();
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fos = new FileOutputStream(file, false);
        OutputStreamWriter osw;
        if (fileEncoding == null) {
            osw = new OutputStreamWriter(fos);
        } else {
            osw = new OutputStreamWriter(fos, fileEncoding);
        }
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(content);
        bw.close();
    }

}
